/**********************************************
*  Classes:   BillboardNode
*  Desc:      Defines a billboard node. Derives
*			  from GeometryNode. Billboards always
*             face the camera.
*  Author:    Vaios Kalpias-Ilias (C) 2008-2009
***********************************************/

#include "BillboardNode.h"
#include "CdnRenderer.h"
#include "Camera.h"
using namespace mth;
//---------------------------------------------------
/**
* Ctor
*/
BillboardNode::BillboardNode()
{
}
//---------------------------------------------------
/**
* Dtor
*/
BillboardNode::~BillboardNode()
{
}
//---------------------------------------------------
/**
* Creates a quad
*/
bool BillboardNode::CreateMesh(CdnRenderer* renderer)
{
	Vector3 *vertices = new Vector3[6];
	Vector3 *normals = new Vector3[6];
	Vector2 *texCoords = new Vector2[6];

	bool ok = vertices && normals && texCoords;
	if (ok)
	{
		vertices[0] = Vector3(-0.5f, 0.5f, 0.0f);
		vertices[1] = Vector3(-0.5f, -0.5f, 0.0f);
		vertices[2] = Vector3(0.5f, 0.5f, 0.0f);
		vertices[3] = Vector3(0.5f, 0.5f, 0.0f);
		vertices[4] = Vector3(-0.5f, -0.5f, 0.0f);
		vertices[5] = Vector3(0.5f, -0.5f, 0.0f);

		normals[0] = Vector3(0.0f, 0.0f, 1.0f);
		normals[1] = normals[0];
		normals[2] = normals[0];
		normals[3] = normals[0];
		normals[4] = normals[0];
		normals[5] = normals[0];

		texCoords[0] = Vector2(0.0f, 1.0f);
		texCoords[1] = Vector2(0.0f, 0.0f);
		texCoords[2] = Vector2(1.0f, 1.0f);
		texCoords[3] = Vector2(1.0f, 1.0f);
		texCoords[4] = Vector2(0.0f, 0.0f);
		texCoords[5] = Vector2(1.0f, 0.0f);

		SetInitialBoundingBox(BoundingBox(0.5f,
			                        -0.5f,
									0.5f,
									-0.5f,
									0.000001f,
									-0.000001f));

		ok = renderer->BindNodeToMesh(this,
		vertices, normals, texCoords, 6);
	}

	if (vertices) delete [] vertices;
	if (normals) delete [] normals;
	if (texCoords) delete [] texCoords;

	return ok;
}
//-------------------------------------------------
/**
* Pre update step
*/
void BillboardNode::PreUpdate()
{
	// face camera
	CameraPtr camera = CdnRenderer::GetInstance()->GetCamera();
	if (camera)
	{
		Vector3 curDir = Vector3(0.0f, 0.0f, 1.0f);
		Vector3 cameraDir = camera->GetGlobalTranslation() -
			GetGlobalTranslation();
		cameraDir.y = 0.0f;
		cameraDir.Normalize();
		m_qGlobalRotation.FromAxisDelta(curDir, cameraDir);
	}

	// inherit transforms
	if (m_pParent)
	{
		if (m_pParent->DerivesFromTransform())
		{
			TransformNode* p = static_cast<TransformNode*>(m_pParent);

			m_vGlobalPos = p->GetGlobalTranslation()
				+ p->GetGlobalRotationQuat().Rotate(m_vLocalPos);
		}
		else
		{
			m_vGlobalPos = m_vLocalPos;
		}
	}
	else
	{
		m_vGlobalPos = m_vLocalPos;
	}

	ResetBoundingBox();
	m_BoundingBox.Move(m_vGlobalPos, m_qGlobalRotation);

	UpdateProperties();
	UpdateLights();
	/*
	m_qLocalRotation = mth::Quaternion();
	GeometryNode::PreUpdate();
	*/
}
//-------------------------------------------------
/**
* Post update step
*/
void BillboardNode::PostUpdate()
{


	GeometryNode::PostUpdate();

}